#include<bits/stdc++.h>
using namespace std;
// #define int long long
int n,fa[400005],cnt[400005],cnt2[400005];
bool vis[400005];
int find(int u){
	if(fa[u]==u)return u;
	return fa[u]=find(fa[u]);
}
signed main(){
	freopen("pairint.in","r",stdin);
	freopen("pairint.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=2*n;i++)fa[i]=i,cnt[i]=1,cnt2[i]=0;
	for(int i=1;i<=n;i++){
		int x,y;
		cin>>x>>y;
		vis[x]=vis[y]=1;
		x=find(x);y=find(y);
		cnt2[y]++;
		if(x==y)continue;
		cnt[y]+=cnt[x];cnt2[y]+=cnt2[x];
		fa[x]=y;
	}
	//
	int ans=0;
	for(int i=1;i<=n*2;i++){
		find(i);
		if(fa[i]==i){
			// cout<<i<<"\n";
			ans+=min(cnt[i],cnt2[i]);
		}
	}
	cout<<ans<<'\n';
	return 0;
}